package org.celery.modules.goods.wrapper;

import org.celery.modules.goods.enums.GoodsStatusEnum;
import org.celery.modules.goods.service.IGoodsService;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.celery.modules.goods.entity.Goods;
import org.celery.modules.goods.vo.GoodsVO;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.system.service.IUserService;

import java.util.List;

/**
 * 商品表包装类,返回视图层所需的字段
 *
 * @author Celery
 * @since 2021-10-26
 */
public class GoodsWrapper extends BaseEntityWrapper<Goods, GoodsVO>  {

    public static GoodsWrapper build() {
        return new GoodsWrapper();
    }

	@Override
	public GoodsVO entityVO(Goods goods) {
		GoodsVO goodsVO = BeanUtil.copy(goods, GoodsVO.class);
		IGoodsService goodsService = SpringUtil.getBean(IGoodsService.class);
		if (Func.isNotEmpty(goods.getCategoryId())) {
			List<String> categoryName = goodsService.getCategoryName(goods.getCategoryId());
			goodsVO.setCategoryName(Func.join(categoryName));
		}
		goodsVO.setStatusName(GoodsStatusEnum.getName(goods.getStatus()));
		IUserService userService = SpringUtil.getBean(IUserService.class);

		goodsVO.setCreateUserName(userService.getById(goods.getCreateUser()).getName());

		return goodsVO;
	}

}
